﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;
//已经作废
namespace HOOMT
{
    class OriginalModelFormDBO : SqliteDataBaseOperator
    {

        RFormToDiagramDBO rdbo = new RFormToDiagramDBO();
        OriginalLineDBO lineDBO = new OriginalLineDBO();
        public bool DeleteForm(OriginalModelForm form)
        {
            try
            {
                this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from OriginalModelForm where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", form.id));
              
                cmd.ExecuteNonQuery();
                rdbo.DeleteRelations(form);

                //删除直线
                if (form.straightlineList != null)
                {
                    foreach (OriginalLine line in form.straightlineList)
                    {
                        lineDBO.DeleteLine(line);
                    }
                } 

                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }
        }


        public bool AddForm(OriginalModelForm form)
        {
            try
            {
                this.DeleteForm(form);
                   this.conn = this.GetConnection();
               SQLiteCommand cmd = this.GetCmd();
               cmd.CommandText = "insert into OriginalModelForm( id ,x ,y,width,height,text,formtype,visible)  "
                   + "values (@id,@x,@y,@width,@height,@text, @formtype,@visible)";
                cmd.Parameters.Add(new SQLiteParameter("id", form.id));
                cmd.Parameters.Add(new SQLiteParameter("x", form.Location.X));
                cmd.Parameters.Add(new SQLiteParameter("y", form.Location.Y));
                cmd.Parameters.Add(new SQLiteParameter("width", form.Size.Width));
                cmd.Parameters.Add(new SQLiteParameter("height", form.Size.Height));
                cmd.Parameters.Add(new SQLiteParameter("text", form.Text));
                cmd.Parameters.Add(new SQLiteParameter("formtype", form.GetType().ToString()));
                cmd.Parameters.Add(new SQLiteParameter("visible", form.Visible.ToString()));


                cmd.ExecuteNonQuery();
                rdbo.AddRelations(form);

                //添加直线
                if (form.straightlineList!=null)
                {
                    foreach (OriginalLine line in form.straightlineList)
                    {
                        lineDBO.AddLine(line, form.id);
                        Console.WriteLine("add a line");
                    }
                } 
               

                //if (form.diagramList != null)
                //{
                //    foreach (OriginalDiagram od in form.diagramList)
                //    {
                //        SQLiteCommand cmd1 = this.GetCmd();
                //        cmd1.CommandText = "insert into  r_form_diagram(id,formid,diagramid) values (@id,@formid,@diagramid)";
                //        cmd1.Parameters.Add(new SQLiteParameter("id", System.Guid.NewGuid()));
                //        cmd1.Parameters.Add(new SQLiteParameter("formid", form.id));
                //        cmd1.Parameters.Add(new SQLiteParameter("diagramid", od.Id));

                //        cmd.ExecuteNonQuery();
                //    }
                //}
                
                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }



        }

        public OriginalModelForm GetFormById(String id, MainForm mainForm)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from OriginalModelForm where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    OriginalModelForm form;
                    if ("ObjectModelForm".Equals(this.GetSimpleType(reader["formtype"].ToString())))
                    {
                  //      Console.WriteLine("1111111111");
                        form = new ObjectModelForm(mainForm);

                        form.TopLevel = false;
                        form.Parent = mainForm;
                        mainForm.splitContainer3.Panel2.Controls.Add(form);


                        form.id = id;
                        Point p = new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()));
                        form.Location = p;
                        Size size = new Size(int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                        form.Text = reader["text"].ToString();
                        form.Visible = bool.Parse(reader["visible"].ToString());
                      //  Console.WriteLine("form.Visible is"+form.Visible);


                        form.diagramList = rdbo.GetRelations(form);

                        //得到直线列表
                        form.straightlineList = lineDBO.GetLineListByParentForm(form);

                        return form;
                    }
                    else if ("StateTransitionModelForm".Equals(this.GetSimpleType(reader["formtype"].ToString())))
                    {
                        form = new StateTransitionModelForm(mainForm);

                        form.TopLevel = false;
                        form.Parent = mainForm;
                        mainForm.splitContainer3.Panel2.Controls.Add(form);


                        form.id = id;
                        Point p = new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()));
                        form.Location = p;
                        Size size = new Size(int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                        form.Text = reader["text"].ToString();
                        form.Visible = bool.Parse(reader["visible"].ToString());
                     //   Console.WriteLine("form.Visible is" + form.Visible);


                        form.diagramList = rdbo.GetRelations(form);
                        //得到直线列表
                        form.straightlineList = lineDBO.GetLineListByParentForm(form);

                        return form;
                    }
                    else if ("InformationFlowModelForm".Equals(this.GetSimpleType(reader["formtype"].ToString())))
                    {
                        form = new InformationFlowModelForm(mainForm);


                        form.TopLevel = false;
                        form.Parent = mainForm;
                        mainForm.splitContainer3.Panel2.Controls.Add(form);

         
                        form.id = id;
                        Point p = new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()));
                        form.Location = p;
                        Size size = new Size(int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                        form.Text = reader["text"].ToString();
                        form.Visible = bool.Parse(reader["visible"].ToString());
                   //     Console.WriteLine("form.Visible is" + form.Visible);


                        form.diagramList = rdbo.GetRelations(form);
                        //得到直线列表
                        form.straightlineList = lineDBO.GetLineListByParentForm(form);

                        return form;
                    }
                    else 
                    {
                        return null;
                    }
                    
                   

                }
                else
                {
                    return null;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }


        }

     
        























//应该是不用的方法
        public List<OriginalDiagram> GetDiagramListByFormId(String formId)
        {
            List<OriginalDiagram> list = new List<OriginalDiagram>();
            SQLiteCommand cmd = GetCmd();
            cmd.CommandText = "select * from r_form_diagram where formid=@formid";
            cmd.Parameters.Add(new SQLiteParameter("formid", formId));
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                String diagramId = reader["diagramId"].ToString();
                String diagramType = reader["diagramType"].ToString();
           //     String simpleDiagramType = this.GetSimpleType(diagramType);
                //String diagramDBOType = diagramType + "DBO";
                //Object diagramDBO = System.Reflection.Assembly.Load(this.projectName).CreateInstance(diagramDBOType);
                
            //    list.Add(diagramDBO)
                //反射调用方法
                //不用反射了直接写
                if ("HighOrderObjectDiagram".Equals(this.GetSimpleType(diagramType)))
                {
                
                    HighOrderObjectDiagramDBO dbo = new HighOrderObjectDiagramDBO();
                    HighOrderObjectDiagram diagram= dbo.GetHOODById(diagramId);
                    list.Add(diagram);
                } 
                else
                {
                }
            }
            
            return list;
        }
        


    }
}
